﻿using CommonManagement.Applcation.Command;
using ERPPublic;
using ERPPublic.eneity;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using ProcurementManagement.Applcation.Command.ProvinceCommand;
using ProcurementManagement.Applcation.Command.VendorCommand;
using ProcurementManagement.Applcation.Command.VendorTypeCommand;
using ProcurementManagement.Domain.Dtos;
using ProcurementManagement.Domain.ProcurementDomain;
using ProcurementManagement.Dtos;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;

namespace ProcurementManagement.Controllers
{
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class VendorController : ControllerBase
    {
        protected readonly IMediator mediatorp;

        public VendorController(IMediator mediatorp)
        {
            this.mediatorp = mediatorp;
        }
        /// <summary>
        ///供应商添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> VendorAdd(VendorCreateCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                cd.AddDtos.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                cd.AddDtos.AddName = name;
                cd.AddDtos.AddTime = DateTime.Now;
                //获取用户ID
                var userId = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
                cd.AddDtos.User_Id = int.Parse(userId);
                result.Code = 200;
                result.Message = "添加供应商成功！";
                result.Data = await mediatorp.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;


           
           
            
        }


        /// <summary>
        /// 供应商分类显示树形
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<VendorTypeDto>>> ProductTypeSelect([FromQuery] VendorTypeShowCommand command)
        {
            ApiResult<List<VendorTypeDto>> result = new ApiResult<List<VendorTypeDto>>();
            try
            {
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        ///行业分类
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<VendorTypePidDto>>> ProductTypePidSelect([FromQuery] VendorTypeShowPidCommand command)
        {
            ApiResult<List<VendorTypePidDto>> result = new ApiResult<List<VendorTypePidDto>>();
            try
            {
                result.Code = 200;
                result.Message = "行业分类显示成功！";
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        ///供应商列表
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<VendorShowDtos>>> ProductTypeSelectShow([FromQuery] VendorShowCommand command)
        {
            ApiResult<PageDTO<VendorShowDtos>> result = new ApiResult<PageDTO<VendorShowDtos>>();
            try
            {
                result.Code = 200;
                result.Message = "供应商显示成功！";
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        ///供应商批量删除
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> VendorDel(VendorDelCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                cd.DelName = name;
                result.Code = 200;
                result.Message = "供应商批量删除成功！";
                result.Data = await mediatorp.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;

        }

        /// <summary>
        ///供应商反填
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Vendor>>> ProductTypeBackfillShow([FromQuery]VendorBackfillCommand command)
        {
            ApiResult<List<Vendor>> result = new ApiResult<List<Vendor>>();
            try
            {
                result.Code = 200;
                result.Message = "供应商反填成功！";
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="cd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> VendorUpd(VendorUpdCommand cd)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                cd.UpdDtos.UpdName = name;
                cd.UpdDtos.UpdTime = DateTime.Now;
                result.Code = 200;
                result.Message = "修改供应商成功！";
                result.Data = await mediatorp.Send(cd);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;





        }

        /// <summary>
        /// 省份
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Province>>> ProvinceSelect([FromQuery] ProvinceCommand command)
        {
            ApiResult<List<Province>> result = new ApiResult<List<Province>>();
            try
            {
                result.Code = 200;
                result.Message = "省份显示成功！";
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 城市
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<City>>> CitySelect([FromQuery] CityCommand command)
        {
            ApiResult<List<City>> result = new ApiResult<List<City>>();
            try
            {
                result.Code = 200;
                result.Message = "省份显示成功！";
                result.Data = await mediatorp.Send(command);
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Code = -1;
                throw;
            }
            return result;
        }








    }
}
